package com.itheima.controller;

import com.itheima.common.result.PageResult;
import com.itheima.common.result.Result;
import com.itheima.pojo.dto.CheckgroupDTO;
import com.itheima.pojo.dto.CheckgroupPageQueryDTO;
import com.itheima.pojo.dto.CheckitemDTO;
import com.itheima.pojo.dto.CheckitemPageQueryDTO;
import com.itheima.service.CheckgroupService;
import com.itheima.service.CheckitemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 检查组Controller
 *
 * @author hjc
 * @date 2025-01-11
 */
@RestController
@RequestMapping("/checkgroup")
@Api(tags = "检查组相关接口")
@Slf4j
public class CheckgroupController {

    @Autowired
    private CheckgroupService checkgroupService;

    /**
     * 查询检查组列表
     */
    @PostMapping("/findPage")
    @ApiOperation("检查组分页查询")
    public Result<PageResult> page(@RequestBody CheckgroupPageQueryDTO checkgroupPageQueryDTO) {
        log.info("检查组分页查询:{}",checkgroupPageQueryDTO);
        PageResult pageResult = checkgroupService.pageQuery(checkgroupPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 新增检查组
     * @param checkgroupDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增检查组")
    public Result add(@RequestBody CheckgroupDTO checkgroupDTO,@RequestParam List<Long> checkitemIds)
    {
        log.info("新增检查组：{}",checkgroupDTO);
        checkgroupService.save(checkgroupDTO,checkitemIds);

        return Result.success();
    }

    /**
     * 根据id查询检查组
     * @param id
     * @return
     */
    @GetMapping("/findById")
    @ApiOperation("根据id查询检查组")
    public Result<CheckgroupDTO> getById(@RequestParam(value = "id") Long id) {
        log.info("根据id查询检查组:{}",id);
        CheckgroupDTO checkgroupDTO = checkgroupService.getById(id);
        return Result.success(checkgroupDTO);
    }



    @PostMapping("/edit")
    @ApiOperation("修改检查组")
    public Result update(@RequestParam List<Long> checkitemIds,@RequestBody CheckgroupDTO checkgroupDTO) {
        log.info("修改检查组：{}",checkgroupDTO);
        checkgroupService.update(checkgroupDTO,checkitemIds);

        return Result.success();
    }

    /**
     * 检查组删除
     * @param id
     * @return
     */
    @GetMapping("/deleteById")
    @ApiOperation("检查组删除")
    public Result delete(@RequestParam Long id) {
        log.info("检查组删除:{}",id);
        checkgroupService.delete(id);

        return Result.success();
    }

}
